package com.ruoyi.packag.service.lmpl;

import cn.jy.common.index.Count;
import cn.jy.common.index.LineChart;
import com.ruoyi.packag.domain.ExpressList;
import com.ruoyi.packag.domain.Message;
import com.ruoyi.packag.domain.Package;
import com.ruoyi.packag.domain.Timeline;
import com.ruoyi.packag.mapper.PackageMapper;
import com.ruoyi.packag.service.PackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
public class Packageservicelmpl implements PackageService {
    @Autowired
    private PackageMapper packageMapper;
    @Autowired
    private ThreadPoolTaskExecutor goodsThreadPool;

    @Override
    public List<Package> show(Package p) {
        List<Package> show = packageMapper.show(p);
        long kaishi = System.currentTimeMillis();
        log.info("开始时间：{}ms",kaishi);
        log.info("开始时间消耗的时间：{}ms", (System.currentTimeMillis() - kaishi));
        show.forEach(a->{
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> a.setTimeline(packageMapper.cha1(a.getTimeX())), this.goodsThreadPool);
            CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() ->a.setExpressVendingMachines(packageMapper.cha2(a.getCabinetid())), this.goodsThreadPool);
            CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> a.setExpressList(packageMapper.cha3(a.getDeliveryid())), this.goodsThreadPool);
            CompletableFuture<Void> voidCompletableFuture3 = CompletableFuture.runAsync(() -> a.setExpressPaymentRecords(packageMapper.cha4(a.getPaymentid())), this.goodsThreadPool);
            CompletableFuture<Void> voidCompletableFuture4 = CompletableFuture.runAsync(() -> a.setExpressAccount(packageMapper.cha5(a.getExtractorid())), this.goodsThreadPool);
            CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1, voidCompletableFuture2, voidCompletableFuture3,voidCompletableFuture4).join();
            log.info("每次遍历消耗的时间：{}ms", (System.currentTimeMillis() - kaishi));
        });
        log.info("结束时间消耗的时间：{}ms", (System.currentTimeMillis() - kaishi));
        return show;
    }

    @Override
    public void chong(Package p) {
        packageMapper.chong(p);
    }

    @Override
    public void tian(Package p) {
        packageMapper.tian(p.getExpressList().getId());
    }

    @Override
    public Count deliveryCount() {
        return packageMapper.deliveryCount();
    }

    @Override
    public List<LineChart> findLineChartDelivery() {
        return packageMapper.findLineChartDelivery();
    }

    @Override
    public Count sendCount() {
        return packageMapper.sendCount();
    }

    @Override
    public List<LineChart> findLineChartSend() {
        return packageMapper.findLineChartSend();
    }

    @Override
    public void add(Message message) {
        packageMapper.add(message);
    }
}
